/**
 * Created with IntelliJ IDEA.
 * Description:
 * Date: 2025-01-31
 * Time: 15:07
 */
public class LinkedList {

    static class ListNode {
        public int val;
        public ListNode next;
        public ListNode prev;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;
    public ListNode last;

    //头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
            last = node;
        } else {
            node.next = head;
            head.prev = node;
            head = node;
        }
    }

    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
            last = node;
        } else {
            last.next = node;
            node.prev = last;
            last = node;
        }
    }

    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index, int data) {
        ListNode node = new ListNode(data);
        if (index > size() || index < 0) {
            return false;
        }
        if (index == 0) {
            addFirst(data);
            return true;
        }
        if (index == size()) {
            addLast(data);
            return true;
        }

        ListNode cur = findIndex(index);

        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
        return true;

    }

    private ListNode findIndex(int index) {
        ListNode cur = head;
        while (index != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                if (cur == head) {
                    head = head.next;

                    if (head == null) {
                        //说明只有一个节点
                        last = null;
                        return;
                    }

                    head.prev = null;
                } else if (cur == last) {
                    last = last.prev;
                    last.next = null;
                } else {
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
                return;
            } else {
                cur = cur.next;

            }
        }
    }

    //删除所有值为key的节点
    public void removeAllKey(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                if (cur == head) {
                    head = head.next;

                    if (head == null) {
                        //说明只有一个节点
                        last = null;
                        return;
                    }

                    head.prev = null;
                } else if (cur == last) {
                    last = last.prev;
                    last.next = null;
                } else {
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
                cur = cur.next;
            }
        }
    }

    //得到单链表的长度
    public int size() {
        int count = 0;
        ListNode cur = head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }

    public void clear() {
        ListNode cur = head;
        while (cur != null) {
            ListNode curN = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curN;
        }
        head = null;
        last = null;
    }

    public ListNode partition(ListNode pHead, int x) {
        ListNode bs = null;
        ListNode be = null;

        ListNode as = null;
        ListNode ae = null;

        ListNode cur = pHead;

        while (cur != null) {
            if (cur.val < x) {
                if (bs == null) {
                    bs = be = cur;
                } else {
                    be.next = cur;
                    be = be.next;
                }
            } else {
                if (as == null) {
                    as = ae = cur;
                } else {
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        if (bs == null) {
            return as;
        }
        be.next = as;
        if (as != null) {
            ae.next = null;
        }

        return bs;
    }

    public boolean chkPalindrome(ListNode head) {
        if (head == null) {
            return false;
        }
        if (head.next == null) {
            return true;
        }
        //找到链表一半的位置
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        //翻转
        ListNode cur = slow.next;
        while (cur != null) {
            ListNode curN = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curN;
        }
        //head从头往后，slow从后往前
        while (head != slow) {
            if (slow.val != head.val) {
                return false;
            }
            //偶数情况
            if (head.next == slow) {
                return true;
            }

            slow = slow.next;
            head = head.next;
        }
        return true;
    }

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        //1.假定A链表长，B链表短
        ListNode pl = headA;
        ListNode ps = headB;
        //2.分别求两个链表的长度
        int len1 = 0;
        int len2 = 0;
        while (pl != null) {
            len1++;
            pl = pl.next;
        }
        while (ps != null) {
            ps = ps.next;
            len2++;

        }
        pl = headA;
        ps = headB;
        //3.求长度的差值
        int len = len1 - len2;
        if (len < 0) {
            len = len2 - len1;
            pl = headB;
            ps = headA;
        }
        //4.确定pl指向的节点一定是长链表 ps指向的节点一定是短链表
        //5.让pl走len步
        while (len != 0) {
            pl = pl.next;
            len--;
        }
        //6.ps和pl同时走 直到相遇
        while (ps != pl) {
            ps = ps.next;
            pl = pl.next;
        }

        if (pl == null) {
            return null;
        }

        return pl;

    }

    public boolean hasCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (slow == fast) {
                return true;
            }
        }
        return false;
    }

    public ListNode detectCycle(ListNode head){
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null){
            fast= fast.next.next;
            slow = slow.next;
            if (fast == slow){
                break;
            }
        }

        if (fast ==null||fast.next==null){
            return null;
        }

        fast = head;
        while (fast != slow){
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }
}